package com.sxc.study;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

/**
 * Create by sunxunchao on 2019/10/9
 */
public class DHPttReceiver extends BroadcastReceiver {
    private static final String TAG = "DHPttReceiver";

    public static final String ACTION_KEY_PRESS = "android.dahua.action.KEY_PRESS";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_KEY_PRESS.equals(action)) {
            int keyCode = intent.getIntExtra("android.dahua.extra.KEY_CODE", 0);
            boolean pressStatus = intent.getBooleanExtra("android.dahua.extra.PRESS_STATUS", false);
            boolean longPress = intent.getBooleanExtra("android.dahua.extra.LONG_PRESS", false);
            Log.d(TAG, "onReceive: pressStatus " + pressStatus);
            Log.d(TAG, "onReceive: longPress " + longPress);
            Log.d(TAG, "onReceive: press " + keyCode);
        } else {
            Log.d(TAG, "onReceive: press error");
        }
    }

    public static void registerReceiver(Context context, DHPttReceiver receiver) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_KEY_PRESS);
        context.registerReceiver(receiver, filter);
    }

    public static void unregisterReceiver(Context context, DHPttReceiver receiver) {
        context.unregisterReceiver(receiver);
    }
}
